package com.kaikeba.wx.controller;

import com.kaikeba.bean.BootStrapTableExpress;
import com.kaikeba.bean.Express;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.mvc.ResponseView;
import com.kaikeba.service.ExpressService;
import com.kaikeba.util.DateformatUtil;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class QRCodeController {
    //直接跳转地址
    @ResponseView("/wx/createQRCode.do")
    public String createQrcode(HttpServletRequest request, HttpServletResponse response){
        User wxUser = UserUtil.getWxUser(request.getSession());
        String userPhone = wxUser.getUserPhone();
        String type = request.getParameter("type");
        String qRCodeContent = null;
        if ("single".equals(type)){   //单个快递信息
            //快递二维码，展示单个快递信息
            //code
            //获取取件码
            String code = request.getParameter("code");
            qRCodeContent = "express_"+code;
        }else if ("list".equals(type)){ //快递信息列表
            //用户二维码，被扫后展示快递员（柜子）所有快递
            //userPhone
            qRCodeContent = "user_"+userPhone;
        }

        //把二维码要存储的内容存到Session中
        HttpSession session = request.getSession();
        //把信息存到二维码中
        session.setAttribute("qrcode",qRCodeContent);
        return "/personQRcode.html";
    }

    @ResponseBody("/wx/qrcode.do")
    public String getQRCode(HttpServletRequest request, HttpServletResponse response){
        //从session中获取二维码要存储的内容
        String qrcode = (String) request.getSession().getAttribute("qrcode");
        Message msg = new Message();
        if (qrcode==null){
            msg.setStatus(-1);
            msg.setResult("取件码获取出错，请用户重新操作");
        }else {
            msg.setStatus(0);
            msg.setResult(qrcode);
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/updateStatus.do")
    public String updateExpressStatus(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        boolean flag = ExpressService.updateStatus(code);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("取件成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("取件码不存在，请更新二维码");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        System.out.println(code);
        Express express = ExpressService.findByCode(code);
        BootStrapTableExpress express1 = null;
        if (express != null) {
            express1 = new BootStrapTableExpress(express.getId(), express.getNumber(), express.getUsername(), express.getUserPhone(), express.getCompany(), express.getCode(),
                    DateformatUtil.format(express.getInTime()), express.getOutTime()==null?"未出库":DateformatUtil.format(express.getOutTime()), express.getStatus()==0?"待取件":"已取件", express.getSysPhone());
        }
        Message msg = new Message();
        if (express == null){
            msg.setStatus(-1);
            msg.setResult("取件码不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(express1);
        }
        System.out.println(express1);
        return JSONUtil.toJSON(msg);
    }
}
